package com.allinpay.merchantweb.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletInputStream;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allinpay.merchantweb.common.utils.StringUtil;
import com.allinpay.merchantweb.constants.CommonConstants;
import com.allinpay.merchantweb.model.CommRoleInfo;
import com.allinpay.merchantweb.model.Manager;
import com.allinpay.merchantweb.service.ITradeQueryService;
import com.google.gson.Gson;

@Controller
@RequestMapping(value="/trade/query")
public class TradeQueryController {

	@Autowired
	ITradeQueryService tradeQueryService;
	
	@GetMapping(value="")
	public String index(HttpServletRequest httpRequest) {
		Manager manager = (Manager) httpRequest.getSession(false).getAttribute(CommonConstants.SESSIONMANAGER);
		if( manager.getUserType() == Manager.USER_TYPE_ALLINPAY_ADMIN) {
			return "trade/details/tradeDetail";
		}else {
			return "trade/details/tradeDetailForMcht";
		}
		
	}
	
	@GetMapping(value="queryFinanceDetails")
	@ResponseBody
	public Object queryFinanceDetails(HttpServletRequest httpRequest) {
		byte[] bytes = new byte[0];
		ServletInputStream inputStream;
		try {
			inputStream = httpRequest.getInputStream();
			bytes = new byte[inputStream.available()];
			inputStream.read(bytes);
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Map<String, String[]> map = httpRequest.getParameterMap();
		
		String str = new String(bytes);
		System.out.println("queryFinanceDetails commRoleInfo:" +
				str
				);
		System.out.println("queryFinanceDetails map:" +
				new Gson().toJson(map)
				);
		Object retObject = null;
		CommRoleInfo commRoleInfo = new CommRoleInfo();
		//only for testing Code
		String sPageNum = httpRequest.getParameter("page");
		String sPageSize = httpRequest.getParameter("limit");
		String sBankCardNbr = httpRequest.getParameter("bankCardNbr");
		String sProdName = httpRequest.getParameter("prodName");
		String sEnterpriseName = httpRequest.getParameter("enterpriseName");
		String sTradingStatus = httpRequest.getParameter("tradingStatus");
		String sTradeType = httpRequest.getParameter("tradeType");
		String sTradeDateBegin = httpRequest.getParameter("enterpriseName");
		String sTradeDateEnd = httpRequest.getParameter("tradeDateEnd");
		if( !StringUtil.isEmpty(sPageNum) && !StringUtil.isEmpty(sPageSize)) {
			commRoleInfo.setPageNum(Integer.parseInt(sPageNum));
			commRoleInfo.setNumPerPage(Integer.parseInt(sPageSize));
		}
		
		Manager manager = (Manager) httpRequest.getSession(false).getAttribute(CommonConstants.SESSIONMANAGER);
		
		
//		if( commRoleInfo == null) {
//			commRoleInfo = new CommRoleInfo();
//			commRoleInfo.setPageNum(1);
//			commRoleInfo.setNumPerPage(10);
//		}
		try {
			Map<String, Object>  reqMap = new HashMap<String, Object>();
			if ( !StringUtil.isEmpty(sPageSize) ) {
				reqMap.put("pageSize", Integer.parseInt(sPageSize));
			}
			if ( !StringUtil.isEmpty(sPageNum) ) {
				reqMap.put("pageNum", Integer.parseInt(sPageNum));
			}	
			
			
			if ( !StringUtil.isEmpty(sBankCardNbr) ) {
				reqMap.put("bankCardNbr", sBankCardNbr);
			}
			if ( !StringUtil.isEmpty(sProdName) ) {
				reqMap.put("prodName", sProdName);
			}
			if ( !StringUtil.isEmpty(sEnterpriseName) ) {
				reqMap.put("enterpriseName", sEnterpriseName);
			}
			if ( !StringUtil.isEmpty(sTradingStatus) ) {
				reqMap.put("tradingStatus", sTradingStatus);
			}
			if ( !StringUtil.isEmpty(sTradeType) ) {
				reqMap.put("tradeType", sTradeType);
			}
			if ( !StringUtil.isEmpty(sTradeDateBegin) ) {
				reqMap.put("tradeDateBegin", sTradeDateBegin);
			}
			if ( !StringUtil.isEmpty(sTradeDateEnd) ) {
				reqMap.put("tradeDateEnd", sTradeDateEnd);
			}
			
			//allinpay manager instanceId = 0
			if( manager.getUserType() == 1) {
				retObject = tradeQueryService.allinpayQueryFinanceDetails(reqMap);
			} else {
				reqMap.put("instanceId", manager.getInstanceId());
				retObject = tradeQueryService.compQueryFinanceDetails(reqMap);
			}
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return retObject;
	}
	
}
